package com.wkk.nursinghome.controller;

import com.wkk.nursinghome.dao.IWithdrawalDao;
import com.wkk.nursinghome.domain.Checkin;
import com.wkk.nursinghome.domain.Withdrawal;
import com.wkk.nursinghome.service.ICheckinService;
import com.wkk.nursinghome.service.IWithdrawalService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class WithdrawalController {
    //注入Service对象
    @Resource
    private IWithdrawalService withdrawalService;
    //显示所有信息
    @GetMapping("/withdrawal")
    public String selectAll(Model model){
        //获取所有信息
        List<Withdrawal> withdrawals= withdrawalService.selectAll();
        //将获取的信息添加到model中
        model.addAttribute("ws",withdrawals);
        //设置返回的试图名称-->templates/withdrawal.html
        return "withdrawal";
    }
    //删除
    @DeleteMapping("/withdrawal/{id}")
    @ResponseBody
    public String deleteById(@PathVariable Integer id){
        //调用方法判断是否删除成功
        boolean idDeleteSuccess=withdrawalService.deleteWithdrawalById(id);
        if(idDeleteSuccess){
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 显示修改信息页面
     * @return
     */
    @GetMapping("/withdrawal/{id}")
    public String getWithdrawalById(@PathVariable("id")Integer id, Model model) {
        Withdrawal withdrawal = withdrawalService.queryById(id);
        model.addAttribute("w", withdrawal);
        return "updateWithdrawal";
    }

    //修改信息
    @PutMapping("/withdrawal")
    public String update(Withdrawal withdrawal){
        boolean ismodifysuccess=withdrawalService.updateWithdrawal(withdrawal);
        if(ismodifysuccess) {
            return "redirect:/withdrawal";
        }else{
            return "first";
        }
    }
    /**
     * 显示审核页面
     * @return
     */
    @GetMapping("/withdrawal2/{id}")
    public String getWithdrawalById2(@PathVariable("id")Integer id, Model model) {
        Withdrawal withdrawal = withdrawalService.queryById(id);
        model.addAttribute("w", withdrawal);
        return "updateWithdrawal2";
    }
    //显示新增信息页面
    @GetMapping("/addWith")
    public String addDept(){
        return "/addWithdrawal";
    }
    //增加信息
    @PutMapping("/addW")
    public String add(Withdrawal withdrawal){
        withdrawalService.addWithdrawal(withdrawal);
        return "redirect:/withdrawal";
    }

    //根据条件查询
    @GetMapping("/searchW")
    public String searchByCondition(Model model, Withdrawal withdrawal){
        List<Withdrawal> withdrawals= withdrawalService.selectByCondition(withdrawal);
        model.addAttribute("ws",withdrawals);
        return "/withdrawal";
    }
}
